"""
Waf tool for functions common to the esp32, Linux and ChibiOS builds.

AP_FLAKE8_CLEAN
"""


def process_hwdef_results(cfg, hwdef_obj):
    # load environment variables
    env = cfg.env
    for k, v in hwdef_obj.env_vars.items():
        if k in env:
            # don't want to encourage modifications from the hwdef
            raise ValueError(f"hwdef redefines environment variable {k} "
                             f"with existing value {env[k]!r} to value {v!r}")
        else:
            env[k] = v
            print("env set %s=%s" % (k, v))

    # note files generated by hwdef system as cfg_files. this ensures they
    # don't get deleted on `./waf clean`. this also ensures their node exists
    # so implicit dependencies are properly tracked.
    bldnode = cfg.bldnode.make_node(cfg.variant)
    for file in sorted(hwdef_obj.output_files):
        env.append_value("cfg_files", bldnode.make_node(file).abspath())
